<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .container {
        width: 600px;
        height: 400px;
        margin: 100px auto;
        overflow: hidden;
        border: 1px solid;
        position: relative;
      }

      .swiper-container {
        position: absolute;
        transition: all 0.5s;
        display: flex;
      }

      .swiper-item {
        width: 600px;
        height: 400px;
        background: #ccc;
        float: left;
      }
      .swiper-item img {
        width: 100%;
        height: 100%;
      }
    </style>
  </head>
  <body>
    <button onclick="change(false)">上一张</button>

    <button onclick="change()">下一张</button>
    <div class="container">
      <div class="swiper-container">
        <div class="swiper-item">
          <img src="swiper/0.jpg" alt="" />
        </div>
        <div class="swiper-item">
          <img src="swiper/1.jpg" alt="" />
        </div>
        <div class="swiper-item">
          <img src="swiper/2.jpg" alt="" />
        </div>
        <div class="swiper-item">
          <img src="swiper/3.jpg" alt="" />
        </div>
        <div class="swiper-item">
          <img src="swiper/4.jpg" alt="" />
        </div>
      </div>
    </div>

    <script>
      let swiperContainer = document.querySelector(".swiper-container");

      let swiperItems = document.querySelectorAll(".swiper-item");

      const width = 600;

      //   swiperContainer.style.width = swiperItems.length * width + "px";

      let index = 0;
      //   let interval = setInterval(change, 3000);

      function change(x = true) {
        if (x) {
          index++;
        } else {
          index--;
        }

        swiperContainer.style.transform = `translateX(-${index * width}px)`;
        if (index === swiperItems.length) {
          index = 0;
        }
      }

      window.addEventListener("keydown", function (e) {
        console.log(e);
        console.log(event);
      });
    </script>
  </body>
</html>
